{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Contents\n",
    "- ## 数据类型与变量\n",
    "- ## 字符串和编码\n",
    "- ## 使用list和tuple\n",
    "- ## 分支与循环\n",
    "- ## 使用Dict和Set"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- # 数据类型与变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10 16 1.23 12300000000.0\n",
      "-----------------\n",
      "I'm fine.\n",
      "hello,\\n\n",
      "world\n",
      "hello,\n",
      "\n",
      "world\n",
      "-----------------\n",
      "0 1 False\n",
      "-----------------\n",
      "1\n",
      "-----------------\n",
      "3.3333333333333335\n",
      "3\n",
      "1\n"
     ]
    }
   ],
   "source": [
    "## 整数，浮点数，字符串，布尔值，空值，变量，常量\n",
    "\n",
    "### 整数，0x[0-f]表示16进制\n",
    "a = 10\n",
    "b = 0x10\n",
    "\n",
    "### 浮点数 超出表示范围为inf（无限大）\n",
    "c = 1.23\n",
    "d = 1.23e10 # 1.23x10^9\n",
    "print(a, b, c, d)\n",
    "print('-----------------')\n",
    "\n",
    "### 字符串\n",
    "print(\"I'm fine.\")\n",
    "print(r'''hello,\\n\n",
    "world''')\n",
    "print('''hello,\\n\n",
    "world''')\n",
    "print('-----------------')\n",
    "\n",
    "### 布尔值True,False and,or,not\n",
    "print(0 and 1, 0 or 1, not True)\n",
    "print('-----------------')\n",
    "\n",
    "### 空值 None\n",
    "\n",
    "### 变量\n",
    "x = 1\n",
    "y = x # y指向的是1这个对象\n",
    "x = 2\n",
    "print(y)\n",
    "print('-----------------')\n",
    "\n",
    "### 常量 一般使用大写变量名表示\n",
    "PI = 3.14159265359\n",
    "\n",
    "### %, /和//\n",
    "print(10 / 3) # 除法结果是浮点数\n",
    "print(10 // 3) # 地板除（整除）\n",
    "print(10 % 3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- # 字符串与编码"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "计算机内存中处理的是unicode，程序读取文本时将编码转为Unicode，保存时再以相应的编码保存；服务器也是类似：动态生成的unicode数据转换为utf-8编码的网页源码\n",
    "之所以用utf-8传输是因为它比unicode节约空间（如果有大量英文的话）\n",
    "\n",
    "Python采用Unicode来编码字符串，所以原生支持多种语言"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### chr() 和 ord()\n",
    "    单个字符可以用ord()获得字符的整数编码，chr()把编码转换成对应字符\n",
    "    知道字符的整数编码可以用十六进制加上\\u前缀来表示字符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "65 27931\n",
      "L 萌\n",
      "-----------------\n",
      "我 喜欢你啊\n",
      "-----------------\n"
     ]
    }
   ],
   "source": [
    "print(ord('A'), ord('洛'))\n",
    "print(chr(76), chr(33804))\n",
    "print('-----------------')\n",
    "\n",
    "print('\\u6211 \\u559c\\u6b22\\u4f60\\u554a')\n",
    "print('-----------------')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 要在网络上传输或者保存到磁盘上，就要把`str`转换成`bytes`："
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'ABC' ABC\n",
      "<class 'bytes'> <class 'str'>\n"
     ]
    }
   ],
   "source": [
    "#print(b'滚来滚去') 错误，因为bytes的每个字符只有一个字节\n",
    "print(b'ABC', 'ABC')\n",
    "print(type(b'ABC'), type('ABC'))# 是不一样的"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### str通过encode()方法可以编码为bytes\n",
    "    (并不能使用unicode编码因为本来就是unicode)\n",
    "    bytes显示时，如果在ASCII字符表内就直接显示，否则显示为\\x##\n",
    "### bytes通过decode()方法可以解码为str\n",
    "    decode的errors=ignore参数可以忽略掉不完整或者是错误的部分，只显示可以解码的字节"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'\\xe5\\x88\\x9d\\xe7\\xba\\xa7\\xe6\\x97\\xa5\\xe6\\x9c\\xac\\xe8\\xaf\\xad'\n",
      "b'A'\n",
      "初级日本语\n",
      "初\n"
     ]
    }
   ],
   "source": [
    "print('初级日本语'.encode('utf-8'))\n",
    "print('A'.encode('utf-8'))\n",
    "\n",
    "by = b'\\xe5\\x88\\x9d\\xe7\\xba\\xa7\\xe6\\x97\\xa5\\xe6\\x9c\\xac\\xe8\\xaf\\xad'\n",
    "print(by.decode('utf-8'))\n",
    "print(b'\\xe5\\x88\\x9d\\xe7\\xba'.decode('utf-8', errors='ignore'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### len()函数计算\n",
    "1. str 的字符数\n",
    "2. bytes 的字节数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2 6\n"
     ]
    }
   ],
   "source": [
    "print(len('中文'), len(b'\\xe4\\xb8\\xad\\xe6\\x96\\x87'))\n",
    "# 可见一个中文字符的bytes占用3个字节"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'\\xe4\\xb8\\xad\\xe6\\x96\\x87'\n",
      "中文\n"
     ]
    }
   ],
   "source": [
    "b=b'\\xe4\\xb8\\xad\\xe6\\x96\\x87'\n",
    "print(b)\n",
    "print(b.decode('utf-8'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 字符串格式化\n",
    "    `%d` 整数\n",
    "    `%f` 浮点数\n",
    "    `%s` 字符串\n",
    "   （**可以一直用，因为有自动类型转换**）\n",
    "   \n",
    "    `%x` 16进制整数\n",
    "\n",
    "`format()`函数： 字符串里面放上{0}, {1}, {2}...占位符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Your grade is 94.500000, Zack.\n",
      "The hex value of 123 is 7b.\n"
     ]
    }
   ],
   "source": [
    "print(\"Your grade is %f, %s.\" % (94.5, 'Zack'))\n",
    "print(\"The hex value of 123 is %x.\" % 123)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "I'm Eric, I'm 18 years old, I'm a boy.\n"
     ]
    }
   ],
   "source": [
    "print(\"I'm %s, I'm %s years old, I'm a %s.\" %('Eric', 18,'boy'))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- # List&Tuple"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### list\n",
    "    append(ele)        在末尾追加ele\n",
    "    insert(index, ele) 在index处插入ele\n",
    "    pop(index=-1)      删除index处元素并返回\n",
    "### tuple\n",
    "    与list十分类似，但是整体不能修改（其中的可变元素可以修改）"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 1, 2, 'c', 'd']\n",
      "['a', 'b', 1, 2, 'c', 'd', 'e']\n",
      "['a', 'b', 1, 2, 'c', 'd', 'e', 'f']\n",
      "None\n"
     ]
    }
   ],
   "source": [
    "## 测试<list>.append()\n",
    "ls=['a','b',1,2,'c','d']\n",
    "print(ls)\n",
    "ls.append(\"e\")\n",
    "print(ls)\n",
    "ls.append('f')\n",
    "print(ls)\n",
    "ls=ls.append('g')\n",
    "## 不可以用ls=ls.append来进行赋值，ls.append已经完成赋值了\n",
    "print(ls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 1, 1.5, 2, 'c', 'd']\n"
     ]
    }
   ],
   "source": [
    "## 测试<list>.insert()\n",
    "ls=['a', 'b', 1, 2, 'c', 'd']\n",
    "## 在1后面添加1.5，1的index=3（start form 0）\n",
    "ls.insert(3,1.5)\n",
    "print(ls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a', 'b', 1, 2, 'c', 'd']\n"
     ]
    }
   ],
   "source": [
    "## 测试<list>.pop删除\n",
    "ls=['a', 'b', 1, 1.5, 2, 'c', 'd']\n",
    "ls.pop(3)\n",
    "print(ls)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['Michael', 'Bob', 'Tracy', 'Adam', 'Monica']\n",
      "['Michael', 'Avalon', 'Bob', 'Tracy', 'Adam', 'Monica']\n",
      "['Michael', 'Avalon', 'Bob', 'Adam', 'Monica'] Tracy\n"
     ]
    }
   ],
   "source": [
    "l1 = ['Michael', 'Bob', 'Tracy', 'Adam']\n",
    "l1.append(\"Monica\")\n",
    "print(l1)\n",
    "l1.insert(1, \"Avalon\")\n",
    "print(l1)\n",
    "p = l1.pop(3)\n",
    "print(l1, p)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- # 分支与循环"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "How old are you?\n",
      "-13\n",
      "Don't go to pornhub.\n"
     ]
    }
   ],
   "source": [
    "age_str = input(\"How old are you?\\n-\")\n",
    "age = int(age_str) ## input()返回str必须转换成int\n",
    "if age < 18:\n",
    "    print(\"Don't go to pornhub.\")\n",
    "elif age < 0:\n",
    "    print(\"WTF?\")\n",
    "else:\n",
    "    print(\"You already see that, didn't you?\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python的两种循环\n",
    "`for x in ...`   `while`\n",
    "Python也支持continue与break"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "You and your friends saw SpongeBob on the road,\n",
      "sadly, HE KNOWS YOU ALL. And Here's the story begins:\n",
      "\n",
      "Hi, Avalon!\n",
      "Hi, LL!\n",
      "Hi, Rocky!\n",
      "Hi, Daisy!\n",
      "Hi, Bob!\n"
     ]
    }
   ],
   "source": [
    "print('''You and your friends saw SpongeBob on the road,\\nsadly, HE KNOWS YOU ALL. And Here's the story begins:\\n''')\n",
    "we = ['Avalon', 'LL', 'Rocky', 'Daisy', 'Bob']\n",
    "for x in we:\n",
    "    print(\"Hi,\", x + '!')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "- # 使用Dict和Set"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### dict （占用内存比list大）\n",
    "    通过key向dict中放入元素\n",
    "    pop(key) 删除键值对\n",
    "    key `in` dict 判断key是否在dict中\n",
    "    get(key, errm) 没有就返回errm\n",
    "### set 无序集合\n",
    "    add() 添加元素\n",
    "    remove() 删除元素\n",
    "    S1 & S2 取交集\n",
    "    S1 | S2 取并集"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "404\n",
      "{1, 3, 4, 6}\n",
      "{1} {0, 1, 3, 4, 5, 6}\n"
     ]
    }
   ],
   "source": [
    "d = {\n",
    "    \"Name\": \"Avalon\",\n",
    "    \"Age\": 19\n",
    "}\n",
    "print(d.get(\"sex\", 404))\n",
    "\n",
    "s1 = set([1, 1, 3, 3, 4, 6])\n",
    "print(s1)\n",
    "print(s1 & {1, 2}, s1 | {5, 0})"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
